package com.webank.wedatasphere.qualitis.question.controller;

import com.webank.wedatasphere.qualitis.controller.request.PageReqDto;
import com.webank.wedatasphere.qualitis.question.entity.Order;
import com.webank.wedatasphere.qualitis.question.service.MyOrderService;
import com.webank.wedatasphere.qualitis.question.service.OrderService;
import com.webank.wedatasphere.qualitis.question.service.dto.AssignDto;
import com.webank.wedatasphere.qualitis.response.Grid;
import com.webank.wedatasphere.qualitis.response.Resp;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 我的工单
 */
@RestController
@RequestMapping("/question/myorder")
public class MyOrderController {
    @Resource
    private OrderService orderService;

    @Resource
    private MyOrderService myOrderService;

    @RequestMapping("/datagrid")
    public Grid<Order> dataGrid(Order order, PageReqDto dto, HttpSession session) {
        // TODO 获取当前用户id过滤工单
        return orderService.dataGrid(order, dto);
    }

    @PostMapping("/transfer")
    public Resp<String> transfer(@RequestBody AssignDto dto) {
        int transferred = myOrderService.transfer(dto.getIds(), dto.getHandler());
        if (transferred == dto.getIds().size()) {
            return Resp.Ok();
        } else {
            return Resp.error("转交失败，请检查工单状态！");
        }
    }

    @PostMapping("/orders")
    public Resp<String> orders(@RequestBody List<Long> ids) {
        int transferred = myOrderService.orders(ids);
        if (transferred == ids.size()) {
            return Resp.Ok();
        } else {
            return Resp.error("接单失败，请检查工单状态！");
        }
    }

    @PostMapping("/complete")
    public Resp<String> complete(Order order) {
        boolean completed = myOrderService.complete(order);
        if (completed) {
            return Resp.Ok();
        } else {
            return Resp.error("完成工单失败，请检查工单状态！");
        }
    }
}
